package com.shaw.workrecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shaw.workrecord.dto.SystemUser;
import com.shaw.workrecord.exception.CustomerException;
import com.shaw.workrecord.mapper.SystemUserMapper;
import com.shaw.workrecord.pojo.request.LoginRequest;
import com.shaw.workrecord.pojo.response.LoginResponse;
import com.shaw.workrecord.service.ILoginService;
import com.shaw.workrecord.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class LoginServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser> implements ILoginService {

    @Autowired
    private SystemUserMapper systemUserMapper;

    @Override
    public LoginResponse login(LoginRequest request) {
        LambdaQueryWrapper<SystemUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.select().eq(SystemUser::getAccount, request.getAccount()).eq(SystemUser::getPassword, request.getPassword());
        SystemUser systemUser = baseMapper.selectOne(wrapper);
        if (systemUser == null) {
            throw new CustomerException("用户不存在");
        }
        HashMap<String,String> claim = new HashMap<>();
        claim.put("id", systemUser.getId().toString());
        //用户校验成功
        String token = JWTUtils.createToken(claim);
        LoginResponse loginResponse = new LoginResponse();
        loginResponse.setToken(token);
        return loginResponse;
    }
}
